﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using AMS.WorkBill.Entities;

namespace AMS.Web.WorkBill
{
    public class WoRuleHelper
    {
        public void SetConditionToSelectAssetSql(WorkBillInfo woInfo,ref string sql)
        {
            if(woInfo==null)
                throw new ArgumentException("工单对象不能为空");
            if(woInfo.TypeName == WorkBillInfo.WO_TYPE_ALLOCATION)
            {
                string strCondition = "";
                if(woInfo.OrgUserGroup!=null)
                {
                   strCondition  = " and OrgUserGroupName='" + woInfo.OrgUserGroup.Name + "'";
                }
                sql = sql.Replace("{0}", strCondition);
            }
        }

        public string CheckWoCommandUpdate(WoCommand comm,string woTypeName,PropertyInfo pi ,object proVal)
        {
            string strErrMsg = "";
            if(comm == null)
            {
                throw new ArgumentException("工单命令行对象不能为空");
            }
            if(woTypeName == WorkBillInfo.WO_TYPE_CAT_VALUE)
            {
                if(pi != null && pi.Name == "TargetOriginalValue" && proVal is double)
                {
                    double targetOriValue = Convert.ToDouble(proVal);
                    if (comm.OrgValue != null && targetOriValue > comm.OrgValue)
                        strErrMsg = "资产【" + comm.AssetInfo.Barcode + "】，目标资产净值【" + targetOriValue + "】不能大于原资产净值【" + comm.OrgValue + "】";
                }
            }
            return strErrMsg;
        }

        public string CheckWoInfoSave(WorkBillInfo woInfo,string woTypeName)
        {
            string strErrMsg = "";
            if (woInfo == null)
                throw new ArgumentException("工单对象不能为空");
            if(woTypeName == WorkBillInfo.WO_TYPE_ALLOCATION)
            {
                if(woInfo.OrgUserGroup != null && woInfo.TargetUserGroup !=null&& woInfo.OrgUserGroup == woInfo.TargetUserGroup)
                {
                    strErrMsg = "工单【" + woInfo.Code + "】，调入部门【" + woInfo.TargetUserGroup.Name + "】不能与调出部门【" + woInfo.OrgUserGroup.Name + "】一致";
                }
            }
            return strErrMsg;
        }
    }
}
